package com.zhangzhan.gulimallware.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhangzhan.gulimallcommcon.util.PageUtils;
import com.zhangzhan.gulimallcommcon.util.Query;
import com.zhangzhan.gulimallware.dao.PurchaseDetailDao;
import com.zhangzhan.gulimallware.entity.PurchaseDetailEntity;
import com.zhangzhan.gulimallware.service.PurchaseDetailService;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseDetailEntity> page = this.page(
                new Query<PurchaseDetailEntity>().getPage(params),
                new QueryWrapper<PurchaseDetailEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageDetail(Map<String, Object> params) {
        String key = (String) params.get("key");
        String status = (String)params.get("status");
        String wareId = (String)params.get("wareId");

        LambdaQueryWrapper<PurchaseDetailEntity> wrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(key)){
            wrapper.and(po->{
                po.eq(PurchaseDetailEntity::getPurchaseId,key)
                        .or()
                        .eq(PurchaseDetailEntity::getSkuId,key);

            });
        }

        wrapper.eq(StrUtil.isNotBlank(status),PurchaseDetailEntity::getStatus,status)
                .eq(StrUtil.isNotBlank(wareId),PurchaseDetailEntity::getWareId,wareId);

        IPage<PurchaseDetailEntity> page = this.page(
                new Query<PurchaseDetailEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

}